const app = getApp();
const util = require('../../utils/util.js');
Page({
  data: {
    showJoin: true, //是否显示单击加入
    userInfo: {},
    hasUserInfo: false,
    showModal: false, //是否显示弹窗 
    shareOpen: false, //是否分享打开此页
    iconList: []
  },
  onLoad: function () {
    let self = this;
    wx.showShareMenu({
      // 是否使用带 shareTicket 的转发
      withShareTicket: true
    });

    let pages = getCurrentPages();
    self.setData({
      shareOpen: pages.length < 2,
      iconList: [{
        imgUrl: app.globalData.apiImgUrl + 'common/sl01.jpg',
        iconText: '加油优惠'
      }, {
        imgUrl: app.globalData.apiImgUrl + 'common/sl09.jpg',
        iconText: '洗车服务'
      }, {
        imgUrl: app.globalData.apiImgUrl + 'common/sl04.jpg',
        iconText: '车辆检测'
      }, {
        imgUrl: app.globalData.apiImgUrl + 'common/sl03.jpg',
        iconText: '代领检字'
      }, {
        imgUrl: app.globalData.apiImgUrl + 'common/sl05.jpg',
        iconText: '违章提醒'
      }, {
        imgUrl: app.globalData.apiImgUrl + 'common/sl06.jpg',
        iconText: '代办验车'
      }, {
        imgUrl: app.globalData.apiImgUrl + 'common/sl07.jpg',
        iconText: '车品福利'
      }, {
        imgUrl: app.globalData.apiImgUrl + 'common/sl08.jpg',
        iconText: '接车定损'
      }, {
        imgUrl: app.globalData.apiImgUrl + 'common/sl02.jpg',
        iconText: '机场停车'
      }]
    });

    // console.log(pages.length)
  },
  onShow: function () {
    let self = this;
    if (self.data.shareOpen) {
      app.getSession().then(function () {
        self.checkUser();
      })
    } else {
      self.checkUser();
    }
  },
  checkUser: function () {
    if (app.globalData.userInfo.VehicleStatus > 0) {
      wx.switchTab({
        url: '/pages/vipCard/vipCard',
      });
      return;
    } else {
      this.setData({
        showJoin: true
      });
    }
  },
  //获取用户手机号码
  getPhoneNumber: function (e) {
    let self = this;
    //异步获取用户信息，所以此处重复判断一遍   
    if (app.globalData.userInfo.UserID > 0) {
      wx.navigateTo({
        url: '/pages/ucenter/getUserInfo/getUserInfo',
      });
      return;
    }

    app.checkSession().then(function () {
      let url = app.globalData.apiRootUrl + '/User/AddUserMoile';
      app.httpRequest({
        url: url,
        showLoading: false,
        method: "POST",
        data: {
          SessionKey: app.globalData.userInfo.SessionKey,
          iv: e.detail.iv,
          encryptedData: e.detail.encryptedData,
          OpenID: app.globalData.userInfo.OpenID,
          UnionID: app.globalData.userInfo.UnionID,
          UserName: app.globalData.userInfo.NickName,
          UserHead: app.globalData.userInfo.UserHead
        },
        success: function (res) {
          let rdata = res.data;
          //console.log(res);
          if (rdata.status == 0) {
            app.globalData.userInfo.UserName = rdata.data.UserName;
            app.globalData.userInfo.UserID = rdata.data.UserID;
            app.globalData.userInfo.UserMobile = rdata.data.UserMobile;
            app.globalData.userInfo.WeChartUserID = rdata.data.WeChartUserID;
            app.setUserInfo(app.globalData.userInfo);
            wx.navigateTo({
              url: '/pages/ucenter/getUserInfo/getUserInfo',
            });
          } else {
            wx.showToast({
              title: '手机号绑定失败，请重新授权',
              icon: 'none'
            });
          }
        }
      });
    });
  },
  //隐藏授权弹窗
  hideModal: function () {
    this.setData({
      showModal: false
    })
  },
  //获取用户信息
  getUserInfo: function (res) {
    let self = this;
    if (res.detail.errMsg == 'getUserInfo:ok') {
      //获取授权码
      app.checkSession().then(function () {
        app.requestUser(res.detail.userInfo).then(
          function () {

          }
        );

        self.hideModal();
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.globalData.shareInfo;
  }
})